﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Data.SqlClient;
using MG_POS_PC.Util;

namespace MG_POS_PC.DAL.Setting
{
    public partial class t_user_setting
    {
        public t_user_setting()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MG_POS_PC.Model.Setting.t_user_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_user_setting(");
            strSql.Append("us_login_id,us_pwd,us_info,us_active,us_color,us_manager)");
            strSql.Append(" values (");
            strSql.Append("@us_login_id,@us_pwd,@us_info,@us_active,@us_color,@us_manager)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@us_login_id", SqlDbType.NVarChar,50),
					new SqlParameter("@us_pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@us_info", SqlDbType.NVarChar,50),
					new SqlParameter("@us_active", SqlDbType.NVarChar,2),
					new SqlParameter("@us_color", SqlDbType.Decimal,5),
					new SqlParameter("@us_manager", SqlDbType.NVarChar,2)};
            parameters[0].Value = model.us_login_id;
            parameters[1].Value = model.us_pwd;
            parameters[2].Value = model.us_info;
            parameters[3].Value = model.us_active;
            parameters[4].Value = model.us_color;
            parameters[5].Value = model.us_manager;

            object obj = DBHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MG_POS_PC.Model.Setting.t_user_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_user_setting set ");
            strSql.Append("us_login_id=@us_login_id,");
            strSql.Append("us_pwd=@us_pwd,");
            strSql.Append("us_info=@us_info,");
            strSql.Append("us_active=@us_active,");
            strSql.Append("us_color=@us_color,");
            strSql.Append("us_manager=@us_manager");
            strSql.Append(" where us_id=@us_id");
            SqlParameter[] parameters = {
					new SqlParameter("@us_login_id", SqlDbType.NVarChar,50),
					new SqlParameter("@us_pwd", SqlDbType.NVarChar,50),
					new SqlParameter("@us_info", SqlDbType.NVarChar,50),
					new SqlParameter("@us_active", SqlDbType.NVarChar,2),
					new SqlParameter("@us_color", SqlDbType.Decimal,5),
					new SqlParameter("@us_manager", SqlDbType.NVarChar,2),
					new SqlParameter("@us_id", SqlDbType.Int,4)};
            parameters[0].Value = model.us_login_id;
            parameters[1].Value = model.us_pwd;
            parameters[2].Value = model.us_info;
            parameters[3].Value = model.us_active;
            parameters[4].Value = model.us_color;
            parameters[5].Value = model.us_manager;
            parameters[6].Value = model.us_id;

            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int us_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user_setting ");
            strSql.Append(" where us_id=@us_id");
            SqlParameter[] parameters = {
					new SqlParameter("@us_id", SqlDbType.Int,4)
			};
            parameters[0].Value = us_id;

            int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string us_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user_setting ");
            strSql.Append(" where us_id in (" + us_idlist + ")  ");
            int rows = DBHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_POS_PC.Model.Setting.t_user_setting GetModel(int us_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 us_id,us_login_id,us_pwd,us_info,us_active,us_color,us_manager from t_user_setting ");
            strSql.Append(" where us_id=@us_id");
            SqlParameter[] parameters = {
					new SqlParameter("@us_id", SqlDbType.Int,4)
			};
            parameters[0].Value = us_id;

            MG_POS_PC.Model.Setting.t_user_setting model = new MG_POS_PC.Model.Setting.t_user_setting();
            DataSet ds = DBHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_POS_PC.Model.Setting.t_user_setting DataRowToModel(DataRow row)
        {
            MG_POS_PC.Model.Setting.t_user_setting model = new MG_POS_PC.Model.Setting.t_user_setting();
            if (row != null)
            {
                if (row["us_id"] != null && row["us_id"].ToString() != "")
                {
                    model.us_id = int.Parse(row["us_id"].ToString());
                }
                if (row["us_login_id"] != null)
                {
                    model.us_login_id = row["us_login_id"].ToString();
                }
                if (row["us_pwd"] != null)
                {
                    model.us_pwd = row["us_pwd"].ToString();
                }
                if (row["us_info"] != null)
                {
                    model.us_info = row["us_info"].ToString();
                }
                if (row["us_active"] != null)
                {
                    model.us_active = row["us_active"].ToString();
                }
                if (row["us_color"] != null && row["us_color"].ToString() != "")
                {
                    model.us_color = int.Parse(row["us_color"].ToString());
                }
                if (row["us_manager"] != null)
                {
                    model.us_manager = row["us_manager"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select us_id,us_login_id,us_pwd,us_info,us_active,us_color,us_manager ");
            strSql.Append(" FROM t_user_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" us_id,us_login_id,us_pwd,us_info,us_active,us_color,us_manager ");
            strSql.Append(" FROM t_user_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DBHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_user_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DBHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.us_id desc");
            }
            strSql.Append(")AS Row, T.*  from t_user_setting T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DBHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_user_setting";
            parameters[1].Value = "us_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
